package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.TblDataHdfsPathManage;
import com.hllxd.hism.star.basic.service.service.HdfsPathService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/hdfsPath")
public class HdfsPathController2 {

    @Autowired
    private HdfsPathService hdfsPathMngService;

    @Data
    public static class Param {
        public Long id;
        public Integer depth;
        public String project;

    }

    @RestGrpcMapping("/list.do")
    public NTSTATUS list(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            List<TblDataHdfsPathManage> items = hdfsPathMngService.listHdfsPath(aa.depth, aa.project);
            log.info("items: {}.", items.size());
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/add.do")
    public NTSTATUS add(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            TblDataHdfsPathManage aa = params.driverObject(TblDataHdfsPathManage.class);
            String userName = params.userID;
            aa.setCreator(userName);
            aa.setLastOperator(userName);
            Object res = hdfsPathMngService.setHdfsPath(aa);
            result.setData(res);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/detail.do")
    public NTSTATUS detail(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            TblDataHdfsPathManage item = hdfsPathMngService.getHdfsPath(aa.id);
            result.setData(item);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
}
